package com.lizhi.menu.common.utils;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalAdjusters;
import java.util.Date;

public class LocalTimeUtil {

    public static final String formatter_data_time = "yyyy-MM-dd HH:mm:ss";
    public static final String formatte_data = "yyyy-MM-dd";
    public static final String formatte_month = "yyyy-MM";
    public static final String formatter_time = "HH:mm:ss";
    public static final String formatte_data_str = "yyMMdd";
    public static final String formatter_mill_time = "HHmmssSSS";
    public static final String month = "MM";
    public static final String formatter_data_str_all = "yyyyMMdd";


    /**
     * 获取当前年月
     *
     * @return yyyy-MM
     */
    public static String getThisMonth() {
        return LocalDateTime.now().format(DateTimeFormatter.ofPattern(formatte_month));
    }

    /**
     * 获取当前时间
     *
     * @return yyyy-MM-dd HH:mm:ss
     */
    public static String getNowDateTime() {
        return LocalDateTime.now().format(DateTimeFormatter.ofPattern(formatter_data_time));
    }

    /**
     * 获取当前时间
     *
     * @return HH:mm:ss
     */
    public static String getNowTime() {
        return LocalDateTime.now().format(DateTimeFormatter.ofPattern(formatter_time));
    }

    /**
     * 获取当前时间
     *
     * @return HH:mm:ss
     */
    public static String getNowMonth() {
        return LocalDateTime.now().format(DateTimeFormatter.ofPattern(formatte_month));
    }

    /**
     * 获取当前时间
     *
     * @return HH:mm:ss
     */
    public static String getLastMonth() {
        return LocalDateTime.now().minusMonths(1L).format(DateTimeFormatter.ofPattern(formatte_month));
    }

    /**
     * 获取当前时间
     *
     * @return yyyy-MM-dd
     */
    public static String getNowDate() {
        return LocalDateTime.now().format(DateTimeFormatter.ofPattern(formatte_data));
    }

    /**
     * 获取当前时间
     *
     * @return yyMMdd
     */
    public static String getNowDateStr() {
        return LocalDateTime.now().format(DateTimeFormatter.ofPattern(formatte_data_str));
    }

    /**
     * 获取当前月份
     *
     * @return yyMMdd
     */
    public static String getMonth() {
        return LocalDateTime.now().format(DateTimeFormatter.ofPattern(month));
    }

    /**
     * 获取当前时间
     *
     * @return yyyyMMdd
     */
    public static String getNowDateAll() {
        return LocalDateTime.now().format(DateTimeFormatter.ofPattern(formatter_data_str_all));
    }

    /**
     * 获取当前时间
     *
     * @return HH:mm:ss
     */
    public static String getNowMillTime() {
        return LocalDateTime.now().format(DateTimeFormatter.ofPattern(formatter_mill_time));
    }

    /**
     * 获取当前日期
     *
     * @return
     */
    public static String getNowDay() {
        return LocalDate.now().format(DateTimeFormatter.ofPattern(formatte_data));
    }

    /**
     * 获取前一天时间
     *
     * @return
     */
    public static String getNowTimeCalendar() {
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime dayPlusBeforeFormat = now.minus(1, ChronoUnit.DAYS);
        return dayPlusBeforeFormat.format(DateTimeFormatter.ofPattern(formatter_data_time));
    }

    /**
     * 获取当月开始时间
     *
     * @return
     */
    public static String getFirstDayOfMonth() {
        LocalDate date = LocalDate.now();
        LocalDate with = date.with(TemporalAdjusters.firstDayOfMonth());
        return LocalDateTime.of(with, LocalTime.MIN).format(DateTimeFormatter.ofPattern(formatter_data_time));
    }

    /**
     * 获取当月开始时间
     *
     * @return
     */
    public static String getLastDayOfMonth() {
        LocalDate date = LocalDate.now();
        LocalDate with = date.with(TemporalAdjusters.firstDayOfMonth());
        return LocalDateTime.of(with, LocalTime.MAX).format(DateTimeFormatter.ofPattern(formatter_data_time));
    }

    /**
     * 获取当月开始时间
     *
     * @return
     */
    public static LocalDateTime getMonthFirstDay() {
        LocalDate date = LocalDate.now();
        LocalDate with = date.with(TemporalAdjusters.firstDayOfMonth());
        return LocalDateTime.of(with, LocalTime.MIN);
    }

    /**
     * 获取当月开始时间
     *
     * @return
     */
    public static LocalDateTime getMonthLastDay() {
        LocalDate date = LocalDate.now();
        LocalDate with = date.with(TemporalAdjusters.firstDayOfMonth());
        return LocalDateTime.of(with, LocalTime.MIN);
    }

    /**
     * 获取当天起始时间
     *
     * @return
     */
    public static String getPresentTimeStart() {
        String dayStartTime = " 00:00:00";
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern(formatte_data);
        return LocalDate.now().format(dtf) + dayStartTime;
    }

    /**
     * 获取当天结束时间
     *
     * @return
     */
    public static String getPresentTimeEnd() {
        String dayEndTime = " 23:59:59";
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern(formatte_data);
        return LocalDate.now().format(dtf) + dayEndTime;
    }

    /**
     * 获取当天开始时间
     *
     * @return
     */
    public static LocalDateTime getStartTimeDay() {
        return LocalDate.now().atStartOfDay();
    }

    /**
     * 时间戳转日期
     *
     * @param time
     * @description: stampToDate
     * @author: xian
     * @date: 2022-11-11 13:26
     * @return: String
     */
    public static LocalDateTime stampToDate(Long time) {
//        time = time * 1000L;
//        Date date = new Date(time);
        return LocalDateTime.ofInstant(new Date().toInstant(), ZoneId.systemDefault());
    }
}
